﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Views_module_search : System.Web.UI.UserControl
{
    protected string keyWord;
    protected int product_id = 0;
    protected int category_id = 0;
    protected int des = 0;
    protected int pri = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        divResult.Visible = false;
        if (Request.QueryString["action"] == "search")
        {
            if (!IsPostBack)
            {
                LoadQueryString();
                getCategory();
            }
            if (!string.IsNullOrEmpty(Request.QueryString["keyword"]))
                SearchResult();
        }        
    }

    public void SearchResult()
    {
        divResult.Visible = true;

        if (!string.IsNullOrEmpty(product_id.ToString()))
            getProductByID(product_id);
        if (!string.IsNullOrEmpty(keyWord))
            searchProduct();
    }

    public void searchProduct()
    {
        productBus proBus = new productBus();
        DataTable dt = new DataTable();

        if (!String.IsNullOrEmpty(keyWord) && category_id == 0 && des == 0 && pri == 0)
            dt = proBus.getProductByName(keyWord);
        else if (!string.IsNullOrEmpty(keyWord) && category_id != 0 && des == 0 && pri == 0)
            dt = proBus.getProductByNameACategoryID(keyWord, category_id);
        else if (!string.IsNullOrEmpty(keyWord) && category_id != 0 && des == 0 && pri != 0)
            dt = proBus.getProductByNameACategoryIDAPrice(keyWord, category_id, pri);
        else if (!string.IsNullOrEmpty(keyWord) && category_id == 0 && des != 0 && pri == 0)
            dt = proBus.getProductByDes(keyWord);
        else if (!string.IsNullOrEmpty(keyWord) && category_id != 0 && des != 0 && pri == 0)
            dt = proBus.getProductByDesACategoryID(keyWord, category_id);
        else if (!string.IsNullOrEmpty(keyWord) && category_id != 0 && des != 0 && pri != 0)
            dt = proBus.getProductByDesACategoryIDAPrice(keyWord, category_id, pri);

        Product_Result.DataSource = dt;
        Product_Result.DataBind();
    }

    public void getProductByID(int product_id)
    {
        productBus probus = new productBus();
        DataTable product = probus.getProductByID(product_id);
        Product_Result.DataSource = product;
        Product_Result.DataBind();
    }

    public void getCategory()
    {
        CategoryBus cateBus = new CategoryBus();
        DataTable dt = new DataTable();
        dt = cateBus.GetAllCategory();
        if (dt.Rows.Count > 0)
        {
            categoryList.DataSource = dt;
            categoryList.DataTextField = "category_name";
            categoryList.DataValueField = "category_id";
            categoryList.DataBind();
            int ccCount = categoryList.Items.Count;
            categoryList.Items.Add("Tìm trong thư mục");
            categoryList.SelectedIndex = ccCount;
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sSearch = "default.aspx?action=search";
        if (!string.IsNullOrEmpty(txtAutoCompleteSearch.Value.ToString()))
            sSearch += "&keyword=" + txtAutoCompleteSearch.Value.ToString();
        if (categoryList.SelectedIndex != categoryList.Items.Count - 1)
        {
            sSearch += "&category=" + categoryList.SelectedValue;
            if (category_id >0)
                categoryList.SelectedValue = category_id.ToString();
        }
        if (description.Checked)
            sSearch += "&des=1";
        if (price.SelectedIndex > 0)
            sSearch += "&price=" + price.SelectedValue;

        Response.Redirect(sSearch);
    }

    public void LoadQueryString()
    {
        if (!String.IsNullOrEmpty(Request.QueryString["keyword"]))
            keyWord = Request.QueryString["keyword"];
        if (!String.IsNullOrEmpty(Request.QueryString["product"]))
            product_id = Convert.ToInt32(Request.QueryString["product"]);
        if (!String.IsNullOrEmpty(Request.QueryString["category"]))
            category_id = Convert.ToInt32(Request.QueryString["category"]);
        if (!String.IsNullOrEmpty(Request.QueryString["des"]))
            des = Convert.ToInt32(Request.QueryString["des"]);
    }
}